﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Visio = Microsoft.Office.Interop.Visio;
using SPNetworkTopology.ExternalWindows.ViewModel;

namespace SPNetworkTopology
{
    class Initialize
    {
        //Visio events object
        VisioEvents visEventsObj;

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="application">Visio application</param>
        public Initialize(Visio.Application application)
        {
            //Initialize
            VisioObjects.visApplication = application;
            VisioObjects.visDocument = application.ActiveDocument;

            VisioObjects.visDocumentList = new List<Visio.Document>();
            VisioObjects.visDocRibbonCollection = new Dictionary<Visio.Document, SharepointNetworkTopologyRibbon>();

            ServiceLinking.SelectedService = ServiceLinking.AvailableServices.ToList();

            visEventsObj = new VisioEvents();
            
            //Hook the events
            this.HookVisioEvents();
        }

        /// <summary>
        /// Subscribe to Visio Events
        /// </summary>
        private void HookVisioEvents()
        {
            VisioObjects.visApplication.DocumentCreated += new Visio.EApplication_DocumentCreatedEventHandler(visEventsObj.visApplication_DocumentCreatedOpened);
            VisioObjects.visApplication.DocumentOpened += new Visio.EApplication_DocumentOpenedEventHandler(visEventsObj.visApplication_DocumentCreatedOpened);
            VisioObjects.visApplication.BeforeDocumentClose += new Visio.EApplication_BeforeDocumentCloseEventHandler(visEventsObj.visApplication_BeforeDocumentClose);
        }

        /// <summary>
        /// Unsubscribe to Visio Events
        /// </summary>
        public void DeleteVisioEvents()
        {
            VisioObjects.visApplication.DocumentCreated -= new Visio.EApplication_DocumentCreatedEventHandler(visEventsObj.visApplication_DocumentCreatedOpened);
            VisioObjects.visApplication.DocumentOpened -= new Visio.EApplication_DocumentOpenedEventHandler(visEventsObj.visApplication_DocumentCreatedOpened);
            VisioObjects.visApplication.BeforeDocumentClose -= new Visio.EApplication_BeforeDocumentCloseEventHandler(visEventsObj.visApplication_BeforeDocumentClose);
        }        
    }
}
